package com.imooc.demo.web;


import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.imooc.demo.entity.Category2;
import com.imooc.demo.service.Category2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//RestController是Controller和ResponseBody的组合
@RestController
@RequestMapping("/category2")//指定路由
public class Category2Controller {

    @Autowired
    private Category2Service category2Service;
    //获取所有种类的信息，value = "listcategory2"是访问url路由
    @RequestMapping(value = "listcategory2",method = RequestMethod.GET)
        private Map<String , Object> listCategory2(){
        Map<String , Object>modelMap = new HashMap<String , Object>();
        List<Category2> list = category2Service.getCategory2List();
        modelMap.put("category2List" , list);
        return  modelMap;
    }

    //通过种类信息获取种类信息
    @RequestMapping(value = "getcategory2byid",method = RequestMethod.GET)
        private  Map<String,Object>  getCategory2ById(Integer category2Id){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        Category2 category2= category2Service.getCategory2ById(category2Id);
        modelMap.put("category2",category2);
        return modelMap;
    }

    //添加种类信息
    @RequestMapping(value = "/addcategory2",method = RequestMethod.POST)
    //@RequestBody标识接收的内容除了xwww from url encoding 编码内容之外的一些内容，如json，xml等,此处需传入xml
    private  Map<String,Object>  addCategory2(@RequestBody Category2 category2){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        modelMap.put("success",category2Service.addCategory2(category2));
        return modelMap;
    }

    //修改种类信息
    @RequestMapping(value = "/modifycategory2",method = RequestMethod.POST)
    //@RequestBody标识接收的内容除了xwww from url encoding 编码内容之外的一些内容，如json，xml等
        private  Map<String,Object>  modifyCategory2(@RequestBody Category2 category2)
            throws JsonParseException, JsonMappingException, IOException {
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        //修改种类信息 ，"success"保存操作状态，如category2Service.modifyCategory2(category2)操作成功返回true，否则false
        modelMap.put("success",category2Service.modifyCategory2(category2));
        return modelMap;
    }

    //删除种类信息
    @RequestMapping(value = "/removecategory2",method = RequestMethod.POST)
    //@RequestBody标识接收的内容除了xwww from url encoding 编码内容之外的一些内容，如json，xml等,此处需传入xml
        private  Map<String,Object>  removeCategory2(Integer category2Id){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        //修改种类信息 ，"success"保存操作状态，如category2Service.deleteCategory2(category2Id)操作成功返回true，否则false
        modelMap.put("success",category2Service.deleteCategory2(category2Id));
        return modelMap;
    }


    //通过种类信息获取种类信息
    @RequestMapping(value = "getcategory2bycategory1id",method = RequestMethod.GET)
    private  Map<String,Object>  getCategory2ByCategory1Id(Integer category10Id){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        List<Category2> category2By1Id = category2Service.getCategory2ByCategory1Id(category10Id);
        modelMap.put("category2By1Id",category2By1Id);
        return modelMap;
    }


   /** @RequestMapping("/weixiu-category")
    public Object productcategoryHtml() {
        return "html/weixiu-category";
    }
    @RequestMapping("/weixiu-list")
    public Object productlistHtml() {
        return "html/weixiu-list";
    }*/


   //模糊搜素
   @RequestMapping(value = "searchcategory2",method = RequestMethod.GET)
   private  Map<String,Object>  searchCategory2(@RequestParam("category2Name") String category2Name){
       Map<String,Object>  modelMap = new HashMap<String ,Object>();
       List<Category2> category2s= category2Service.searchCategory2(category2Name);

           modelMap.put("category2s",category2s);


       return modelMap;
   }

}
